const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const app = express();
const ApiConfig=require('./apiconfig')

// 中间件
app.use(cors()); // 允许跨域
app.use(bodyParser.json()); // 解析JSON请求体




// 登录接口
app.post('/api/user/login', (req, res) => {
  const { phone, code } = req.body;

  // 验证输入
  if (!phone || !code) {
    return res.status(200).json({ 
      success: false,
      message: '用户名和密码不能为空' 
    });
  }

 

  if (code=='111111') {
    // 登录成功（实际项目中应该返回token）
    res.json({ 
      success: true,
      message: '登录成功',
      data: { phone:phone,code:code ,name:'tom',age:10,edulevl:'本科',token:'dasjeqwn123nxhaiweq'}
    });
  } else {
    // 登录失败
    res.status(401).json({ 
      success: false,
      message: '用户名或密码错误' 
    });
  }
});
// 树结构
app.get(ApiConfig.subjectlist, (req, res) => {
    return res.status(200).json({ 
        success: true,
        data:[
            {
                "title":"react",
                "value":"react",
                "children":[
                    {
                       "title":"21天通关react",
                        "value":"123122", 
                    },  {
                       "title":"21天通关react-redux",
                        "value":"123121322", 
                    },
                ]
            },{
                "title":"vue",
                "value":"vue",
                "children":[
                    {
                       "title":"3天通关vue3",
                        "value":"878662", 
                    },  {
                       "title":"vue2从入门到精通",
                        "value":"1232211", 
                    },
                ]
            },
        ]
      });
})
// 启动服务器
const PORT = 7001;
app.listen(PORT, () => {
  console.log(`服务器运行在 http://localhost:${PORT}`);
});